﻿using System;
using System.ServiceModel;

namespace XchangeStreamer.Utilities
{
    public static class Settings
    {
        #region Public Members

        public const string Action = "Send";
        public const string Name = "Send";
        public const string Namespace = "http://www.softideas.co";
        public const int CompressionThreshold = 54;
        
        #endregion

        #region Const.

        static Settings()
        {
            InitTcpBinding();
            InitNamedPipeBinding();
        }

        #endregion

        #region Public Prop.

        public static System.ServiceModel.Channels.Binding TcpBinding
        {
            get;
            private set;
        }
        
        public static System.ServiceModel.Channels.Binding NamedPipeBinding
        {
            get;
            private set;
        }

        #endregion

        #region Private Methods

        private static void InitTcpBinding()
        {
            var binding = new NetTcpBinding(SecurityMode.None, true);
            
            binding.Namespace = Settings.Namespace;
            binding.OpenTimeout = TimeSpan.FromDays(1);
            binding.CloseTimeout = TimeSpan.FromDays(1);
            binding.SendTimeout = TimeSpan.FromDays(1);
            binding.ReceiveTimeout = TimeSpan.FromDays(5);
            
            binding.Security.Mode = SecurityMode.None;
            binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
            binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
            
            binding.ListenBacklog = int.MaxValue;
            binding.MaxBufferPoolSize = int.MaxValue;
            binding.MaxBufferSize = int.MaxValue;

            /*
             * (Not Supported on Mono)
             * 
            binding.MaxConnections = 1000;
            */

            binding.MaxReceivedMessageSize = int.MaxValue;
            binding.PortSharingEnabled = false;
            binding.TransferMode = TransferMode.Buffered;

            binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
            binding.ReaderQuotas.MaxBytesPerRead = int.MaxValue;
            binding.ReaderQuotas.MaxDepth = int.MaxValue;
            binding.ReaderQuotas.MaxNameTableCharCount = int.MaxValue;
            binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;

            /*
             * (Not Supported on Mono)
             * 
            binding.ReliableSession.Enabled = true;
            binding.ReliableSession.InactivityTimeout = TimeSpan.FromDays(5);
            binding.ReliableSession.Ordered = true;
            */

            binding.ReliableSession.Enabled = false;
            binding.ReliableSession.InactivityTimeout = TimeSpan.FromDays(5);
            binding.ReliableSession.Ordered = true;

            TcpBinding = binding;
        }

        private static void InitNamedPipeBinding()
        {
            var binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);

            binding.Namespace = Settings.Namespace;
            binding.OpenTimeout = TimeSpan.FromDays(1);
            binding.CloseTimeout = TimeSpan.FromDays(1);
            binding.SendTimeout = TimeSpan.FromDays(1);
            binding.ReceiveTimeout = TimeSpan.FromDays(5);

            binding.Security.Mode = NetNamedPipeSecurityMode.None;
            binding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.None;

            binding.MaxBufferPoolSize = int.MaxValue;
            binding.MaxBufferSize = int.MaxValue;

            /*
             * (Not Supported on Mono)
             * 
            binding.MaxConnections = 1000;
            */

            binding.MaxReceivedMessageSize = int.MaxValue;
            binding.TransferMode = TransferMode.Buffered;

            binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
            binding.ReaderQuotas.MaxBytesPerRead = int.MaxValue;
            binding.ReaderQuotas.MaxDepth = int.MaxValue;
            binding.ReaderQuotas.MaxNameTableCharCount = int.MaxValue;
            binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;

            /*
             * (Not Supported on Mono)
             * 
            binding.ReliableSession.Enabled = true;
            binding.ReliableSession.InactivityTimeout = TimeSpan.FromDays(5);
            binding.ReliableSession.Ordered = true;
            */

            NamedPipeBinding = binding;
        }

        #endregion
    }
}
